package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingPlanDto;
import com.zzyl.entity.NursingPlan;
import com.zzyl.entity.NursingProjectPlan;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.mapper.NursingProjectPlanMapper;
import com.zzyl.queryparam.NursingPlanQueryParam;
import com.zzyl.service.NursingPlanService;
import com.zzyl.vo.NursingPlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class NursingPlanServiceImpl implements NursingPlanService {

    @Autowired
    private NursingPlanMapper nursingPlanMapper;

    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;


    @Override
    public PageResponse<NursingPlanVo> getPage(NursingPlanQueryParam npqp) {
        //设置分页参数
        PageHelper.startPage(npqp.getPageNum(), npqp.getPageSize());

        //查询数据
        List<NursingPlan> nps = nursingPlanMapper.getPage(npqp);

        for (NursingPlan np : nps) {
            //获取项目id
            Long npId = np.getId();

            //根据id查询是否有关联护理计划
            List<NursingProjectPlan> counts = nursingPlanMapper.getCount(npId);

            //根据集合的长度可以知道是否有关联护理计划 （0=没有）
            np.setCount(counts.size());
        }

        Page<NursingPlan> page = (Page<NursingPlan>) nps;

        //数据封装
        return PageResponse.of(page, NursingPlanVo.class);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void addNursingPlan(NursingPlanDto npd) {

        NursingPlan np = BeanUtil.toBean(npd, NursingPlan.class);
        np.setStatus(1);
        nursingPlanMapper.addNursingPlan(np);

        //获取护理计划Id
        Integer npId = Math.toIntExact(np.getId());

        //添加护理计划绑定项目表数据
        List<NursingProjectPlan> projectPlanList = npd.getProjectPlans();
        if (!CollectionUtils.isEmpty(projectPlanList)) {
            for (NursingProjectPlan nursingProjectPlan : projectPlanList) {
                nursingProjectPlan.setPlanId(npId);
            }
            nursingProjectPlanMapper.addNursingProjectPlan(projectPlanList);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public NursingPlanVo getByIdNursingPlan(Long id) {

        //根据id查询护理计划
        NursingPlan np = nursingPlanMapper.getByIdNursingPlan(id);

        //实体类对象 转换成 VO对象
        NursingPlanVo nursingPlanVo = BeanUtil.toBean(np, NursingPlanVo.class);

        //根据护理计划id查询护理计划绑定项目表
        List<NursingProjectPlan> nursingProjectPlanList = nursingProjectPlanMapper.getByIdNursingProjectPlan(id);

        //给VO对象的护理计划绑定项目表赋值
        nursingPlanVo.setProjectPlans(nursingProjectPlanList);

        return nursingPlanVo;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateNursingPlan(NursingPlanDto npd) {
        //DTO对象 转换成 实体类对象
        NursingPlan nursingPlan = BeanUtil.toBean(npd, NursingPlan.class);

        //修改护理计划
        nursingPlanMapper.updateNursingPlan(nursingPlan);

        //获取护理计划Id 根据护理计划Id删除老的护理计划绑定项目表
        Integer planId = Math.toIntExact(nursingPlan.getId());
        nursingProjectPlanMapper.deleteNursingProjectPlan(planId);

        //添加从前端传来的新的护理计划绑定项目表数据
        List<NursingProjectPlan> projectPlanList = npd.getProjectPlans();
        if (!CollectionUtils.isEmpty(projectPlanList)) {
            for (NursingProjectPlan nursingProjectPlan : projectPlanList) {
                nursingProjectPlan.setPlanId(planId);
            }
            nursingProjectPlanMapper.addNursingProjectPlan(projectPlanList);
        }
    }

    @Override
    public void statusNursingPlan(Integer id, Integer status) {
        nursingPlanMapper.statusNursingPlan(id, status);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteNursingPlan(Integer id) {
        //删除护理计划
        nursingPlanMapper.deleteNursingPlan(id);
        //删除护理计划项目关联表
        nursingProjectPlanMapper.deleteNursingProjectPlan(id);
    }

    @Override
    public List<NursingPlanVo> allList() {
        //查询所有的护理计划
        List<NursingPlan> npList = nursingPlanMapper.allList();

        //VO对象集合
        List<NursingPlanVo> npvList = new ArrayList<>();

        //把实体类对象 转换成 VO对象 存入VO对象集合
        for (NursingPlan nursingPlan : npList) {
            NursingPlanVo nursingPlanVo = BeanUtil.toBean(nursingPlan, NursingPlanVo.class);
            npvList.add(nursingPlanVo);
        }

        return npvList;
    }
}
